import request from '@/dvadmin/request/index';



const BUTTON_VALUE_TO_COLOR_MAPPING = {
  1: 'success',
  true: 'success',
  0: 'danger',
  false: 'danger',
  Search: 'warning', // 查询
  Update: 'primary', // 编辑
  Create: 'success', // 新增
  Retrieve: 'info', // 单例
  Delete: 'danger' // 删除
}

function getButtonSettings (objectSettings) {
  return objectSettings.map(item => {
    return {
      label: item.label,
      value: item.value,
      color: item.color || BUTTON_VALUE_TO_COLOR_MAPPING[item.value]
    }
  })
}


const state = {
	shareInfo: {}, // 默认分享数据
	settings: {},
	pcaCode: [],//区域
	unreadMsg: 0,
	dictData: {}
}
const getters = {
	shareInfo: state => state.shareInfo,
	settings: state => state.settings,
	pcaCode: state => state.pcaCode,
	unreadMsg: state => state.unreadMsg,
	dictData: state => state.dictData
}

const mutations = {
	shareInfo(state, shareInfo) {
		state.shareInfo = shareInfo;
	},
	pcaCode(state, pcaCode) {
		state.pcaCode = pcaCode;
	},
	unreadMsg(state, unreadMsg) {
		state.unreadMsg = unreadMsg
	},
	setDict(state,data){
		state.dictData = data
	}
}
const actions = {
	// 初始化数据
	async appInit({
		commit,
		dispatch
	}, options) {
		request({
			url: '/api/init/settings/',
			method: 'get',
			params: {
				key: 'login|b_end_marketing'
			},
			custom: {
				noBack: true,
				auth: false
			}
		}).then(res => {
			state.settings = res.data
		})
		return false;
	},
	async getPcaCode({
		commit,
		dispatch,
		state
	}) {
		if (state.pcaCode[0]) return
		uni.request({
			url: 'https://kfm-waiter.oss-cn-zhangjiakou.aliyuncs.com/data/pca-code.json',
			success: (res) => {
				commit('pcaCode', res.data)
			}
		});
	},
	/**
	 * 设置未读消息
	 */
	setUnreadMsg({
		commit,
		dispatch
	}, number) {
		commit('unreadMsg', number)
	},
	/**
	 * 获取字典
	 */
	async getDict({
		commit,
		dispatch
	}, key='all') {
		request({
			url: '/api/init/dictionary/',
			method: 'get',
			params: {
				dictionary_key: key
			},
			custom: {
				noBack: true,
				auth: false
			}
		}).then(res => {
			let newData = {}
			if (key === 'all') {
				res.data.data.map(data => {
					data.children.map((children, index) => {
						switch (children.type) {
							case 1:
								children.value = Number(children.value)
								break
							case 6:
								children.value = children.value === 'true'
								break
						}
					})
					newData[data.value] = getButtonSettings(data.children)
				})
				// state.dictData = newData
				commit('setDict',newData)
			} else {
				// state.dictData = res.data.data[key]
				commit('setDict',res.data.data[key])
			}
		})
		return false;
	}
}
export default {
	actions,
	state,
	mutations,
	getters
}
